package com.dmwork.service.interaction.controller.front;

import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.vo.CmsExcellentWorkVo;
import com.dmwork.service.interaction.service.CmsExcellentWorkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/cms_excellent_work")
public class FrontCmsExcellentWorkController {

    @Autowired
    private CmsExcellentWorkService cmsExcellentWorkService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "get_excellent_work_list";

    @ApiOperation("获取所有优秀作品")
    @GetMapping("get_excellent_work_list")
    public R getExcellentWorkList() {
        List<CmsExcellentWorkVo> cmsExcellentWorkVoList = null;
        Boolean hasKey = redisTemplate.hasKey(KEY);
        if (hasKey != null && hasKey) {
            cmsExcellentWorkVoList = (List<CmsExcellentWorkVo>) redisTemplate.opsForValue().get(KEY);
        } else {
            cmsExcellentWorkVoList = cmsExcellentWorkService.getExcellentWorkList();
            if (cmsExcellentWorkVoList != null && cmsExcellentWorkVoList.size() > 0)
                redisTemplate.opsForValue().set(KEY, cmsExcellentWorkVoList);
        }
        return R.ok().data("rows", cmsExcellentWorkVoList);
    }

}
